/**
 * <p>Package : org.tea.springdata.mina</p>
 * <p>E-Mail : ztlyde@gmail.com</p>
 */
package org.tea.springdata.mina;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoAcceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import org.tea.springdata.mina.config.MinaConfig;
import org.tea.springdata.mina.handler.ServerHandler;

/**
 * @author: John zhang
 * @version: 0.1
 */
@Component
public class NioSocketServer {

	private static final int port = 4848;

	@Autowired
	@Qualifier("nioSocketAcceptor")
	IoAcceptor serverAcceptor;

	@Autowired
	ServerHandler handler;
	
	public void start() {
		serverAcceptor.setHandler(handler);
		serverAcceptor.setDefaultLocalAddress(new InetSocketAddress(port));
		
		try {
			serverAcceptor.bind();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main( String args[] ) {
		AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext();
		rootContext.register(MinaConfig.class);
		rootContext.refresh();
		
		NioSocketServer server = (NioSocketServer) rootContext.getBean("nioSocketServer");
		server.start();
	}
	
}
